#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

// 定义Sortable接口
class Sortable {
public:
    virtual void sort() = 0;
    virtual ~Sortable() {}
};

// CustomObject类
class CustomObject {
private:
    string attribute1;
    int attribute2;

public:
    CustomObject(string attr1, int attr2) : attribute1(attr1), attribute2(attr2) {}

    string getAttribute1() const {
        return attribute1;
    }

    int getAttribute2() const {
        return attribute2;
    }

    // 打印CustomObject的内容
    void print() const {
        cout << "attribute1: " << attribute1 << ", attribute2: " << attribute2 << endl;
    }
};

// CustomObjectArray类
class CustomObjectArray : public Sortable {
private:
    vector<CustomObject> objects;
    string sortBy;

public:
    CustomObjectArray(vector<CustomObject> objs, string sortAttr) : objects(objs), sortBy(sortAttr) {}

    void sort() override {
        if (sortBy == "attribute1") {
            std::sort(objects.begin(), objects.end(), [](const CustomObject& a, const CustomObject& b) {
                return a.getAttribute1() < b.getAttribute1();
            });
        } else if (sortBy == "attribute2") {
            std::sort(objects.begin(), objects.end(), [](const CustomObject& a, const CustomObject& b) {
                return a.getAttribute2() < b.getAttribute2();
            });
        } else {
            throw invalid_argument("Unknown sort attribute: " + sortBy);
        }
    }

    // 打印数组中的所有CustomObject
    void printObjects() const {
        for (const auto& obj : objects) {
            obj.print();
        }
    }
};

// 主函数
int main() {
    // 创建CustomObject实例
    vector<CustomObject> customObjects = {
        CustomObject("Object3", 20),
        CustomObject("Object1", 15),
        CustomObject("Object2", 10)
    };

    // 创建CustomObjectArray实例并排序
    CustomObjectArray customArray(customObjects, "attribute2");
    customArray.sort();
    cout << "Sorted CustomObjectArray contents:" << endl;
    customArray.printObjects(); // 打印排序后的数组内容

    return 0;
}
